﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.DAL
{
    public class Authority : Base<Model.Authority>
    {
        public Authority(ISession isession)
            : base(isession)
        {
            
        }

        public override Model.Authority GetModel(object id)
        {
            Model.Authority ret = null;
            var list = Get("ID=?", null, 0, 0, id);
            if (list.Count > 0)
            {
                ret = list[0];
            }
            return ret;
        }

        public Model.Authority GetModelByWebSite(Common.EnumType.RoleUserType UserType, string RoleUserCode, string WebSiteCode)
        {
            Model.Authority model = null;
            string where = "UserType=? and RoleUserCode=? and WebSiteCode=? and MenuCode is null and ButtonCode is null";
            object[] ps = { UserType, RoleUserCode, WebSiteCode };
            var list = Get(where, null, 0, 0, ps);
            if (list.Count > 0)
            {
                model = list[0];
            }
            return model;
        }

        public Model.Authority GetModelByMenu(Common.EnumType.RoleUserType UserType, string RoleUserCode,string WebSiteCode,string MenuCode)
        {
            Model.Authority model = null;
            string where = "UserType=? and RoleUserCode=? and WebSiteCode=? and MenuCode=? and ButtonCode is null";
            object[] ps = { UserType, RoleUserCode, WebSiteCode, MenuCode };
            var list = Get(where, null, 0, 0, ps);
            if (list.Count > 0)
            {
                model = list[0];
            }
            return model;
        }

        public Model.Authority GetModelByButton(Common.EnumType.RoleUserType UserType, string RoleUserCode, string WebSiteCode, string MenuCode,string ButtonCode)
        {
            Model.Authority model = null;
            string where = "UserType=? and RoleUserCode=? and WebSiteCode=? and MenuCode=? and ButtonCode=?";
            object[] ps = { UserType, RoleUserCode, WebSiteCode, MenuCode, ButtonCode };
            var list = Get(where, null, 0, 0, ps);
            if (list.Count > 0)
            {
                model = list[0];
            }
            return model;
        }

        public IList<Model.Authority> GetUserAllAuthority(Common.EnumType.ResourceType resourceType, string webSiteCode, string menuCode, string buttonCode, Model.Manager user)
        {
            StringBuilder where = new StringBuilder();
            List<object> ps = new List<object>();

            where.Append("WebSiteCode=?"); ps.Add(webSiteCode);
            where.Append(" and ResourceType=?"); ps.Add(resourceType);
            if (resourceType == Common.EnumType.ResourceType.Menu)
            {
                where.Append(" and MenuCode=?"); ps.Add(menuCode);
            }
            else if (resourceType == Common.EnumType.ResourceType.Button)
            {
                where.Append(" and MenuCode=?"); ps.Add(menuCode);
                where.Append(" and ButtonCode=?"); ps.Add(buttonCode);
            }
            where.Append(" and ((it.UserType=? and it.RoleUserCode=?)"); ps.Add(Common.EnumType.RoleUserType.User); ps.Add(user.UserName);
            if (user.ManagerRoles!=null && user.ManagerRoles.Count > 0)
            {
                StringBuilder roleCodes = new StringBuilder();
                foreach (var role in user.ManagerRoles)
                {
                    roleCodes.Append(",'" + role.RoleCode + "'");
                }
                roleCodes.Remove(0, 1);
                where.Append(" or (it.Type=? and it.RoleUserCode in (" + roleCodes.ToString() + ") )");
                ps.Add(Common.EnumType.RoleUserType.Role);
            }
            where.Append(")");
            var authlist = Get(where.ToString(), null, 0, 0, ps.ToArray());
            return authlist;
        }
    }
}
